package com.itheima.service.db.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.db.SoulConclusion;
import com.itheima.domain.db.SoulDimensions;
import com.itheima.domain.db.SoulQuestionOptions;
import com.itheima.domain.db.SoulQuestions;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.mapper.SoulConclusionMapper;
import com.itheima.mapper.SoulDimensionsMapper;
import com.itheima.mapper.SoulQuestionOptiosMapper;
import com.itheima.mapper.SoulQuestionsMapper;
import com.itheima.service.db.SoulTestService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.awt.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class SoulTestServiceImpl implements SoulTestService {

    @Autowired
    private SoulQuestionsMapper soulQuestionsMapper;

    @Autowired
    private SoulQuestionOptiosMapper soulQuestionOptiosMapper;

    @Autowired
    private SoulConclusionMapper soulConclusionMapper;

    @Autowired
    private SoulDimensionsMapper soulDimensionsMapper;

    //分页查询所有题目
    @Override
    public PageBeanVo findQuestionByPage(Integer page, Integer limit, String startDate, String endDate, String category) {

        IPage<SoulQuestions> iPage = new Page(page, limit);
        QueryWrapper<SoulQuestions> queryWrapper = new QueryWrapper<>();
        if (startDate != null && endDate == null ) {
            Date parse = null;
            try {
                parse = new SimpleDateFormat("yyyy-MM-dd").parse(startDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            queryWrapper.eq("created", parse);
        }

        if (startDate == null && endDate != null) {
            Date parse = null;
            try {
                parse = new SimpleDateFormat("yyyy-MM-dd").parse(endDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            queryWrapper.eq("created", parse);
        }

        if (startDate != null && !StrUtil.equals("",startDate) && endDate != null&& !StrUtil.equals("",endDate)) {
            Date startParse = null;
            Date endParse = null;
            try {
                startParse = new SimpleDateFormat("yyyy-MM-dd").parse(startDate);
                endParse = new SimpleDateFormat("yyyy-MM-dd").parse(endDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            queryWrapper.ge("created", startDate);
            queryWrapper.le("created", endDate);
        }

        if (category != null) {
            queryWrapper.eq("star", Integer.valueOf(category));
        }

        IPage<SoulQuestions> soulQuestionsIPage = soulQuestionsMapper.selectPage(iPage, queryWrapper);

        //封装返回值
        PageBeanVo pageBeanVo = new PageBeanVo(page, limit, soulQuestionsIPage.getTotal(), soulQuestionsIPage.getRecords());

        return pageBeanVo;
    }

    //根据问题id查询对应的选项
    @Override
    public List<SoulQuestionOptions> findOptionsByQuestionId(Long questionId) {
        //拼接条件
        QueryWrapper<SoulQuestionOptions> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("question_id", questionId);
        List<SoulQuestionOptions> optionsList = soulQuestionOptiosMapper.selectList(queryWrapper);
        //返回
        return optionsList;
    }

    //根据题目id查询题目详情
    @Override
    public SoulQuestions findQuestionById(Long id) {
        SoulQuestions soulQuestions = soulQuestionsMapper.selectById(id);
        return soulQuestions;
    }

    //删除题目
    @Override
    public void deleteQuestion(Long id) {
        QueryWrapper<SoulQuestions> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        soulQuestionsMapper.delete(queryWrapper);
    }

    //根据选项id删除选项
    @Override
    public void deleteOptionById(Long id) {
        QueryWrapper<SoulQuestionOptions> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        soulQuestionOptiosMapper.delete(queryWrapper);
    }

    //修改题目
    @Override
    public void updateQuestion(SoulQuestions question) {
        QueryWrapper<SoulQuestions> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", question.getId());
        soulQuestionsMapper.update(question, queryWrapper);
    }

    //保存选项
    @Override
    public void saveOptions(SoulQuestionOptions soulQuestionOptions) {
        soulQuestionOptiosMapper.insert(soulQuestionOptions);
    }

    //保存题目
    @Override
    public void saveQuestion(SoulQuestions soulQuestions) {
        soulQuestionsMapper.insert(soulQuestions);
    }

    //根据问题题目和星级查询问题
    @Override
    public SoulQuestions findQuestion(String name, Integer category) {
        QueryWrapper<SoulQuestions> wrapper = new QueryWrapper<>();
        wrapper.eq("question", name);
        wrapper.eq("star", category);
        SoulQuestions soulQuestions = soulQuestionsMapper.selectOne(wrapper);
        return soulQuestions;
    }

    //分页查询报告
    @Override
    public PageBeanVo findTestReport(Integer page, Integer limit) {
        IPage<SoulConclusion> iPage = new Page<>(page, limit);
        QueryWrapper<SoulConclusion> queryWrapper = new QueryWrapper<>();
        iPage = soulConclusionMapper.selectPage(iPage, queryWrapper);
        PageBeanVo pageBeanVo = new PageBeanVo(page, limit, iPage.getTotal(), iPage.getRecords());
        return pageBeanVo;
    }

    //根据id查询dimensions
    @Override
    public SoulDimensions findDimensionsById(Long dimensions) {
        QueryWrapper<SoulDimensions> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", dimensions);
        SoulDimensions soulDimensions = soulDimensionsMapper.selectOne(queryWrapper);
        return soulDimensions;
    }

    //根据报告id查询报告
    @Override
    public SoulConclusion findReportById(Long id) {
        SoulConclusion soulConclusion = soulConclusionMapper.selectById(id);
        return soulConclusion;
    }

    //修改报告表
    @Override
    public void updateConclusion(SoulConclusion conclusion) {
        QueryWrapper<SoulConclusion> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", conclusion.getId());
        soulConclusionMapper.update(conclusion, queryWrapper);
    }

    //修改维度
    @Override
    public void updateDimensions(SoulDimensions soulDimensions) {
        QueryWrapper<SoulDimensions> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", soulDimensions.getId());
        soulDimensionsMapper.update(soulDimensions, queryWrapper);
    }
}
